<template>
  <div>
    <div class="slot">
      <div class="top">
        <div class="return" @click="goback">
          <img src="@/assets/my/zuo.png" alt />
        </div>
        <span>确认订单</span>
      </div>
    </div>
    <div class="main">
      <div class="main_top">
        <div class="top_ul">
          <div
            :class="active == item.id ? 'ul_li1' : 'ul_li'"
            v-for="item in list"
            :key="item.id"
            @click="peistype(item.id)"
          >
            {{ item.name }}
          </div>
        </div>
        <div class="dizhi"> 
          <!-- 仓库自提 -->
          <div v-show="active == 2">
            <div
              class="dizhi_main"
              @click="cangkchange"
              v-show="warehouseLen !== 0"
            >
              <img src="@/assets/pblic/hdizhi.png" alt />
              <div class="info">
                <div class="p">
                  <div class="p1">{{ warehouse ? warehouse.name : "" }}</div>
                  <div class="btn">仓库地址</div>
                </div>
                <div class="p2">
                  {{ warehouse ? warehouse.province : ""
                  }}{{ warehouse ? warehouse.city : ""
                  }}{{ warehouse ? warehouse.area : ""
                  }}{{ warehouse ? warehouse.detail : "" }}
                </div>
              </div>
              <div class="icon">
                <img src="@/assets/pblic/youjiantou.png" alt />
              </div>
            </div>
            <div
              class="dizhi_main"
              @click="cangkchange"
              v-show="warehouseLen === 0"
            >
              <img class="img1" src="@/assets/pblic/zengjia.png" alt />
              <div class="tianjia">选择仓库地址</div>
            </div>
          </div>
          <!-- 同城配送/物流配送 -->
          <div v-show="active == 3 || active == 1">
            <div
              class="dizhi_main"
              @click="changeaddress"
              v-show="Object.keys(this.address).length != 0"
            >
              <img src="@/assets/pblic/hdizhi.png" alt />
              <div class="info">
                <div class="p">
                  <div class="p1">{{ address.name }}</div>
                  <div class="p3">{{ address.phone }}</div>
                </div>
                <div class="p2">
                  {{ address.province }}{{ address.city }}{{ address.area
                  }}{{ address.detail }}
                </div>
              </div>
              <div class="icon">
                <img src="@/assets/pblic/youjiantou.png" alt />
              </div>
            </div>
            <!-- 物流配送无地址 -->
            <div
              class="dizhi_main"
              @click="changeaddress"
              v-show="Object.keys(this.address).length == 0"
            >
              <img class="img1" src="@/assets/pblic/zengjia.png" alt />
              <div class="tianjia">选择收货地址</div>
            </div>
          </div>
        </div>
      </div>
      <div class="main_center" v-for="(item,index) in shopinfo.goods" :key="index">
        <div class="center_cont" >
          <div class="shop1">
            <img :src="item.image" alt />
            <div class="shop_info1">
              <div class="shop_name1">{{ item.title }}</div>
              <div class="shop_xsh1">最低销售价：￥{{ item.price }}</div>
              <div class="shop_xsh1" v-show="item.specs_name">
                已选商品规格：{{ item.specs_name }}
              </div>
              <div class="shop_price1">
                ￥
                <span>{{ item.price }}</span>
              </div>
            </div>
          </div>
          <div class="li">
            <div class="title">商品单价</div>
            <div class="num">￥{{ item.price }}</div>
          </div>
          <div class="li">
            <div class="title">商品数量</div>
            <div class="num">{{ item.num }}</div>
          </div>
        </div>
      </div>
      <div class="main_center">
        <div class="center_cont" >
          <!-- <div class="shop1">
            <img :src="item.image" alt />
            <div class="shop_info1">
              <div class="shop_name1">{{ item.title }}</div>
              <div class="shop_xsh1">最低销售价：￥{{ item.price }}</div>
              <div class="shop_xsh1" v-show="item.specs_name">
                已选商品规格：{{ item.specs_name }}
              </div>
              <div class="shop_price1">
                ￥
                <span>{{ item.price }}</span>
              </div>
            </div>
          </div>
          <div class="li">
            <div class="title">商品单价</div>
            <div class="num">￥{{ item.price }}</div>
          </div>
          <div class="li">
            <div class="title">商品数量</div>
            <div class="num">{{ item.num }}</div>
          </div> -->
          <div class="li">
            <div class="title">运费</div>
            <div class="num">0.00</div>
          </div>
          <div class="li">
            <div class="title">商品总额</div>
            <div class="num">￥{{ shopinfo.total }}</div>
          </div>
          <div class="li1">
            <div class="title">备注</div>
            <textarea
              class="input"
              v-model="beizhu"
              placeholder="请输入备注内容"
              type="text"
            />
          </div>
        </div>
      </div>
    </div>
    <div class="bottombboo"></div>
    <div class="bottom">
      <div class="cd_info">
        <span>
          共{{ shopinfo.num }}，
          <span style="font-size: 0.3rem; color: #000000; line-height: 28px"
            >合计：</span
          >
          <span style="color: #ff511e">
            ￥
            <span
              style="font-size: 0.4rem; line-height: 36px; font-weight: Bold"
              >{{ shopinfo.total }}</span
            >
          </span>
        </span>
      </div>
      <div class="mitbtn" @click="submitorder(1)">提交订单</div>
    </div>
    <div class="zhezhao" v-show="isshow"></div>
    <div class="zhezhao1" v-show="isshow">
      <div class="tankuang">
        <div class="guanb" @click="guanbi">
          <img src="@/assets/pblic/guanbi.png" alt />
        </div>
        <div class="p" v-show="type===1">确定提交订单？</div>
        <div class="btn">
          <div class="btn1" @click="turnquxiao">取消</div>
          <div class="btn2" @click="turn">确定</div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import axios from "axios";
import bus from "@/utils/bus";
import { Dialog } from "vant";
export default {
  data() {
    return {
      order_id: "",
      shopinfo: {},
      active: 0,
      address: null,
      warehouse: [],
      addressid: "",
      cangkid: "",
      beizhu: "",
      token: "",
      goods_specs: {},
      list: [
        { id: 1, name: "物流配送" },
        { id: 3, name: "同城配送" },
        { id: 2, name: "仓库自提" },
      ],
      warehouseLen: 0,
      goaddress: 0,
      type: 0,
      cart_ids:'',
      isshow: false,
    };
  },
  watch: {
    warehouse: function (newVal, oldVal) {
      if (newVal) {
        this.warehouseLen = 1;
      } else {
        this.warehouseLen = 0;
      }
    },
  },
  created() {
    this.token = window.localStorage.getItem("token");
    this.order_id = this.$route.query.order_id;
    this.cart_ids = this.$route.query.cart_ids;
    if (this.$route.query.type) {
      this.active = this.$route.query.type;
    }
    if (this.$route.query.addressid) {
      this.addressid = this.$route.query.addressid;
      axios
        .post("/index/user/ads_info", this.$qs.stringify({
          id: this.addressid,
          token: this.token,
        }))
        .then((res) => {
          if (res.data.code === 1) {
            this.address = res.data.data;
          } else {
            this.$toast(res.data.msg);
          }
        });
    }
    if (this.$route.query.cangkid) {
      this.cangkid = this.$route.query.cangkid;
      axios
        .post("/index/order/warehouse_info", this.$qs.stringify({
          id: this.cangkid,
          token: this.token,
        }))
        .then((res) => {
          if (res.data.code === 1) {
            this.warehouse = res.data.data;
          }
        });
    }
    axios.post("/index/order/info", this.$qs.stringify({
        id: this.order_id,
        token: this.token,
      }))
      .then((res) => {
        if (res.data.code === 1) {
          this.shopinfo = res.data.data;
          this.goods_specs = res.data.goods_specs;
          if (this.address == null) {
            this.address = res.data.data.address;
            this.addressid = res.data.data.address.id;
            console.log('adderss', this.address)
          }
          if (this.warehouse == undefined || this.warehouse.length <= 0) {
            this.warehouse = res.data.warehouse;
          }
        } else {
          this.$toast(res.data.msg);
        }
      });
    axios.post("/index/user/ads_list", this.$qs.stringify({ token: this.token })).then((res) => {
      if (res.data.code === 1) {
        if (res.data.data.length == 0) {
          this.goaddress = 1;
        } else {
          this.goaddress = 0;
        }
      } else {
        this.$toast(res.data.msg);
      }
    });
  },
  methods: {
    goback() {
      this.$router.back();
    },
    peistype(id) {
      this.active = id;
    },
    changeaddress() {
      var type = this.active;
      if (this.goaddress === 1) {
        this.$router.push({
          path: "/addressadd",
          query: { type, order_id: this.order_id , cart_ids: this.cart_ids},
        });
      } else {
        this.$router.replace({
          path: "/myaddress",
          query: { type, order_id: this.order_id , cart_ids: this.cart_ids},
        });
      }
    },
    cangkchange() {
      var type = this.active;
      this.$router.push({
        path: "/storehouse",
        query: { type, order_id: this.order_id , cart_ids: this.cart_ids},
      });
    },
    turn() {
      if (this.type === 1) {
        if (this.addressid == "" && this.cangkid == "") {
          return this.$toast("必须选择收货地址或者仓库地址~");
        }
        axios
          .post("/index/order/confirm", this.$qs.stringify({
            token: this.token,
            order_id: this.order_id,
            num: this.shopinfo.num,
            cart_ids:this.cart_ids,
            is_way: this.active,
            warehouse_id: this.cangkid,
            address_id: this.addressid
              ? this.addressid
              : this.$route.query.addressid,
            remark: this.beizhu,
          }))
          .then((res) => {
            console.log(res.data, 46564);
            if (res.data.code === 1) {
              this.$toast(res.data.msg);
              this.$router.replace("/allorder");
            } else {
              this.$toast(res.data.msg);
            }
          });
      }
    },
    guanbi(){
      this.isshow=false
    },
    turnquxiao(){
      this.isshow=false
    },
    submitorder(i) {
      if(this.active===0){
        this.$toast('请选择配送方式');
      }else{
        this.isshow = true;
      this.type = i;
      }
    },
  },
  beforeCreate() {
    document
      .querySelector("body")
      .setAttribute("style", "background-color:#F8F8F8");
  },
  beforeDestroy() {
    document.querySelector("body").setAttribute("style", "");
  },
};
</script>

<style lang="scss" scoped>
.zhezhao {
  width: 100%;
  height: 100%;
  background: #000000;
  opacity: 0.5;
  position: fixed;
  top: 0;
  left: 0;
  z-index: 19;
}
.zhezhao1 {
  width: 100%;
  height: 100%;
  position: fixed;
  top: 0;
  left: 0;
  z-index: 19;
  display: flex;display: -webkit-flex;
  justify-content: center;
  align-items: center;
  .tankuang {
    width: 310px;
    height: 145px;
    background: #ffffff;
    border-radius: 10px;
    position: relative;
    .guanb {
      position: absolute;
      top: 5px;
      right: 5px;
      img {
        width: 18px;
        height: 18px;
      }
    }
    .p {
      width: 100%;
      font-size: 16px;
      font-family: PingFang SC;
      font-weight: 500;
      color: #000000;
      line-height: 16px;
      text-align: center;
      margin-top: 43px;
    }
    .btn {
      width: 100%;
      display: flex;display: -webkit-flex;
      align-items: center;
      justify-content: center;
      margin-top: 30px;
      .btn1 {
        width: 123px;
        height: 37px;
        background: #dddddd;
        border-radius: 18.5px;
        font-size: 14px;
        font-family: PingFang SC;
        font-weight: 500;
        color: #555555;
        line-height: 37px;
        text-align: center;
      }
      .btn2 {
        width: 123px;
        height: 37px;
        background: #fdd40a;
        border-radius: 18.5px;
        font-size: 14px;
        font-family: PingFang SC;
        font-weight: 500;
        color: #000000;
        line-height: 37px;
        text-align: center;
        margin-left: 24px;
      }
    }
  }
}
.slot {
  width: 100%;
  height: 44px;
  .top {
    width: 100%;
    height: 44px;
    display: flex;display: -webkit-flex;
    justify-content: center;
    align-items: center;
    background: #fdd40a;
    position: fixed;
    top: 0;
    left: 0;
    z-index: 100;
    .return {
      width: 38px;
      height: 100%;
      position: absolute;
      top: 0;
      left: 0;
      display: flex;display: -webkit-flex;
      justify-content: center;
      align-items: center;
      img {
        width: 8px;
        height: 15px;
      }
    }
    span {
      font-size: 18px;
      font-family: PingFang SC;
      font-weight: bold;
      color: #000000;
      line-height: 44px;
    }
  }
}
.main {
  width: 100%;
  background: url("../../assets/pblic/bj4.png") no-repeat center top;
  background-size: 100% 35%;
  .main_top {
    width: 345px;
    margin: auto;
    .top_ul {
      padding-top: 5px;
      display: flex;display: -webkit-flex;
      justify-content: space-between;
      align-items: center;
      .ul_li {
        width: 110px;
        height: 45px;
        background: #ffffff;
        opacity: 0.7;
        border-radius: 10px 10px 0px 0px;
        text-align: center;
        font-size: 15px;
        font-family: PingFang SC;
        font-weight: 500;
        color: #444444;
        line-height: 35px;
      }
      .ul_li1 {
        width: 110px;
        height: 45px;
        background: #ffffff;
        border-radius: 10px 10px 0px 0px;
        text-align: center;
        font-size: 15px;
        font-family: PingFang SC;
        font-weight: bold;
        color: #000000;
        line-height: 35px;
      }
    }
    .dizhi {
      width: 325px;
      background: #ffffff;
      border-radius: 10px;
      margin-top: -10px;
      display: flex;display: -webkit-flex;
      justify-content: center;
      align-items: center;
      padding: 15px 10px;
      .dizhi_main {
        width: 305px;
        padding: 10px;
        background: #fffbe6;
        border: 1px dashed #fdd40a;
        border-radius: 5px;
        display: flex;display: -webkit-flex;
        align-items: center;
        img {
          width: 27px;
          height: 27px;
          margin-right: 10px;
        }
        .tianjia {
          font-size: 16px;
          font-family: PingFang SC;
          font-weight: 500;
          color: #222222;
          line-height: 16px;
        }
        .img1 {
          width: 19px;
          height: 19px;
          margin-right: 10px;
        }
        .info {
          width: 230px;
          .p {
            display: flex;display: -webkit-flex;
            .p1 {
              font-size: 15px;
              font-family: PingFang SC;
              font-weight: 500;
              color: #000000;
              line-height: 15px;
            }
            .p3 {
              font-size: 13px;
              font-family: PingFang SC;
              font-weight: 500;
              color: #888888;
              line-height: 13px;
              margin-left: 5px;
            }
            .btn {
              width: 48px;
              height: 15px;
              background: #4c78fa;
              border-radius: 3px;
              font-size: 11px;
              font-family: PingFang SC;
              font-weight: 500;
              color: #ffffff;
              line-height: 15px;
              text-align: center;
              margin-left: 5px;
            }
          }
          .p2 {
            font-size: 12px;
            font-family: PingFang SC;
            font-weight: 500;
            color: #444444;
            line-height: 14px;
            margin-top: 10px;
          }
        }
        .icon {
          margin-left: 30px;
          img {
            width: 8px;
            height: 14px;
          }
        }
      }
    }
  }
  .main_center {
    width: 345px;
    height: auto;
    margin: auto;
    background: #ffffff;
    box-shadow: 0px 0px 50px 0px rgba(0, 0, 0, 0.06);
    border-radius: 10px;
    margin-top: 8px;
    margin-bottom: 8px;
    .center_cont {
      width: 321px;
      margin: auto;
      .shop1 {
        width: 100%;
        height: 120px;
        display: flex;display: -webkit-flex;
        align-items: center;
        img {
          width: 90px;
          height: 90px;
          margin-right: 10px;
        }
        .shop_info1 {
          .shop_name1 {
            font-size: 14px;
            font-family: PingFang SC;
            font-weight: bold;
            color: #000000;
            line-height: 14px;
          }
          .shop_xsh1 {
            font-size: 12px;
            font-family: PingFang SC;
            font-weight: 500;
            color: #999999;
            line-height: 12px;
            margin-top: 8px;
          }
          .shop_price1 {
            font-size: 12px;
            font-family: PingFang SC;
            font-weight: 500;
            color: #ff511e;
            line-height: 14px;
            margin-top: 30px;
            span {
              font-size: 17px;
              font-family: PingFang-SC-Bold;
              font-weight: Bold;
            }
          }
        }
      }
      .li {
        width: 100%;
        border-top: 1px solid #f8f8f8;
        display: flex;display: -webkit-flex;
        justify-content: space-between;
        align-items: center;
        .title {
          font-size: 14px;
          font-family: PingFang SC;
          font-weight: 500;
          color: #333333;
          line-height: 48px;
        }
        .num {
          font-size: 14px;
          font-family: PingFang SC;
          font-weight: bold;
          color: #333333;
          line-height: 48px;
        }
      }
      .li1 {
        width: 100%;
        border-top: 1px solid #f8f8f8;
        .title {
          font-size: 14px;
          font-family: PingFang SC;
          font-weight: 500;
          color: #333333;
          line-height: 48px;
        }
        .input {
          width: 100%;
          border: none;
          font-size: 14px;
          font-family: PingFang SC;
          font-weight: 500;
          color: #333333;
          line-height: 20px;
        }
      }
    }
  }
}
.bottombboo{
  width: 100%;
  height: 64px;
}
.bottom {
  width: 100%;
  height: 64px;
  position: fixed;
  bottom: 0;
  left: 0;
  display: flex;display: -webkit-flex;
  justify-content: space-between;
  align-items: center;
  background: white;
  .cd_info {
    margin-left: 15px;
    font-family: PingFang SC;
    font-weight: 500;
    font-size: 12px;
    color: #999999;
    line-height: 12px;
  }
  .mitbtn {
    width: 145px;
    height: 44px;
    background: #fdd40a;
    border-radius: 22px;
    font-size: 15px;
    font-family: PingFang SC;
    font-weight: bold;
    color: #222222;
    line-height: 44px;
    text-align: center;
    margin-right: 15px;
  }
}
</style>
